﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Waiter.Controler;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Net.NetworkInformation;

namespace Waiter.Common
{
    /// <summary>
    /// Class define util methods
    /// </summary>
    class CommonUtils
    {
        /// <summary>
        /// Get string from date with format paramenter
        /// </summary>
        /// <param name="format"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string fsFormatDate(String format, DateTime date)
        {
            return String.Format(format, date);
        }

        /// <summary>
        /// get center index of paren control
        /// </summary>
        /// <param name="formWidth"></param>
        /// <param name="objWidth"></param>
        /// <returns></returns>
        public static int fsGetCenterPoint(int formWidth, int objWidth)
        {
            if (objWidth >= formWidth)
            {
                return 0;
            }
            return (formWidth - objWidth) / 2;
        }

        /// <summary>
        /// create login name by staff name
        /// </summary>
        /// <param name="staffName"></param>
        /// <returns></returns>
        public static string fsCreateLoginName(string staffName)
        {
            UserCtrl userCtrl = new UserCtrl();
            if (staffName == null || staffName == "")
            {
                return "";
            }

            // create login name
            staffName = fsTrimSpace(staffName);
            string[] listName = staffName.Split(' ');
            string loginName = listName[0];
            for (int i = 1; i < listName.Length; i++)
            {
                loginName = loginName + listName[i][0];
            }

            // check exist
            if (userCtrl.fsIsExistUser(loginName))
            {
                loginName = loginName + userCtrl.fsCountAllUser(CommonConstants.FLAG_OFF);
            }
            return loginName;
        }

        /// <summary>
        /// remove all spaces by a space
        /// </summary>
        /// <returns></returns>
        public static string fsTrimSpace(string source)
        {
            return Regex.Replace(source.Trim(), @"\s+", string.Empty);
        }

        /// <summary>
        /// Create new code
        /// </summary>
        /// <returns></returns>
        public static string fsCreateNewCode()
        {
            return fsFormatDate(CommonConstants.DDMMYYHHMMSS, DateTime.Now);
        }

        public static string fsCreateMoneyString(long num)
        {
            return num.ToString(CommonConstants.NUMBER_FORMAT1) + " " + CommonConstants.MONEY_CHAR;
        }

        public static string fsGetFirstChars(string str)
        {
            if (str == null || str.Length == 0)
            {
                return "";
            }

            StringBuilder resultStr = new StringBuilder();
            string[] strList = str.Split(' ');
            for (int i = 0; i < strList.Length; i++)
            {
                if (strList[i].Length > 0)
                {
                    resultStr.Append(char.ToUpper(strList[i][0]));
                }
            }

            return resultStr.ToString();
        }

        public static string fsToString(object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }

            return obj.ToString();
        }

        public static string MD5Hash(string text)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();
        }

        public static string GetMacAddress()
        {
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    return nic.GetPhysicalAddress().ToString();
                }
            }
            return "";
        }

        public static string fsGetMachineSerialNumner()
        {
            return MD5Hash(GetMacAddress() + CommonConstants.ENCODE_STRING);
        }

        public static string fsObjectToString(object obj)
        {
            StringBuilder result = new StringBuilder();
            try
            {
                if (obj == null)
                {
                    return string.Empty;
                }

                result.Append(obj.GetType().Name);
                result.Append(" {");
                foreach (var pro in obj.GetType().GetProperties())
                {
                    result.Append("\n\t");
                    result.Append(pro.Name);
                    result.Append(": ");
                    result.Append(pro.GetValue(obj,null));
                }
                result.Append("\n}");
            }
            catch (Exception)
            {
            }
            return result.ToString();
        }

        public static string fsParamListToString(object []paramArray)
        {
            StringBuilder result = new StringBuilder();
            try
            {
                foreach (var obj in paramArray)
                {
                    result.Append("\n->");
                    if (obj.GetType().IsClass 
                        && obj.GetType().Name != "String" 
                        && obj.GetType().Name != "DateTime")
                    {
                        result.Append(fsObjectToString(obj));
                    }
                    else
                    {
                        result.Append(obj);
                    }
                }
            }
            catch (Exception)
            {
            }
            return result.ToString();
        }

        public static string fsGetOPID()
        {
            return Waiter.Properties.Settings.Default.STAFF_LOGIN_CODE;
        }

        public static string fsGetOPName()
        {
            StaffCtrl staffCtrl = new StaffCtrl();
            STAFF item = staffCtrl.fsFindStaffByCode(fsGetOPID());
            if (item == null)
            {
                return string.Empty;
            }
            return item.NAME;
        }
        public static string fsGetOPName(string opID)
        {
            StaffCtrl staffCtrl = new StaffCtrl();
            STAFF item = staffCtrl.fsFindStaffByCode(opID);
            if (item == null)
            {
                return string.Empty;
            }
            return item.NAME;
        }
    }
}
